<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class UserCreateForm extends CFormModel
{
    public $id;
    public $userUuid;
    public $name;
    public $password;
    public $passwordConfirm;
    public $email;
    public $nick;
    public $isAdmin;
    public $space;

    
    public $admin_array = array("0"=>"普通用户","1"=>"管理员");


    public function init(){
        $this->space = Yii::app()->params["app"]["defaultSpace"];        $this->isAdmin =false;    }
    public function rules()
    {
        return array(
        array('name,password,passwordConfirm,space', 'required'),
        array('name', 'length', 'max'=>16),
        array('nick', 'length', 'max'=>255),
        array('email', 'length', 'max'=>255),
        array('space', 'numerical'),
        array('isAdmin', 'numerical', 'integerOnly'=>true),
        array('passwordConfirm', 'compare', 'compareAttribute' => 'password'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'name' => '用户名',
            'nick' => '昵称',
            'password' => '用户密码',
            'passwordConfirm' => '确认密码',
            'email' => '电子邮件',
            'space'=>'可用空间',
            'isAdmin' => '赋予管理员权限',


        );
    }
    
    private function _validateEmail(){
        if(strlen($this->email)>0){
            $validator = new CEmailValidator;
            if($validator->validateValue($this->email)==false){
                $this->addError("email","电子邮件格式不正确");
                return false;
            }
        }
        return true;
    }
    
    private function _validateName(){
        $user = User::model()->find("user_name=?",array($this->name));
        if(isset($user)){
            $this->addError("name",$this->name." 已经存在。");
            return false;
        }
        return true;
    }
    
    public function save(){

        if($this->validate()){
                        if($this->_validateEmail() && $this->_validateName()){
                                $salt = CUtils::genRandomString(6);
                                $user = new User();
                $user["user_uuid"]=uniqid();
                $user["user_name"]=$this->name;
                $user["salt"]     =$salt;
                $user["user_pass"]=CSecurity::signPasswd($this->password, $salt);
                $user->save();
                                if(strlen($this->email)){
                                        $userMeta = new UserMeta();
                    $userMeta["user_id"]=$user["id"];
                    $userMeta["meta_key"]="email";
                    $userMeta["meta_value"]=$this->email;
                    $userMeta->save();
                }
                if(strlen($this->nick)){
                                        $userMeta = new UserMeta();
                    $userMeta["user_id"]=$user["id"];
                    $userMeta["meta_key"]="nick";
                    $userMeta["meta_value"]=$this->nick;
                    $userMeta->save();
                }
                $userMeta = new UserMeta();                $userMeta["user_id"]=$user["id"];
                $userMeta["meta_key"]="is_admin";
                $userMeta["meta_value"]=$this->isAdmin;
                $userMeta->save();
                $userMeta = new UserMeta();                $userMeta["user_id"]=$user["id"];
                $userMeta["meta_key"]="space";
                $userMeta["meta_value"]=$this->space;
                $userMeta->save();
                return true;
            }
        }
        return false;
    }
}